
package com.htd.htdapmservice.xxljob;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.htd.htdapmapi.alarm.request.AlarmInto;
import com.htd.htdapmapi.alarm.result.AlarmResult;
import com.htd.htdapmcommon.constants.ErrorCodeConstants;
import com.htd.htdapmcommon.enums.AlarmSendStatus;
import com.htd.htdapmcommon.enums.AlarmsLevelEnum;
import com.htd.htdapmcommon.enums.AlarmsStatusEnum;
import com.htd.htdapmdmo.LogAnalyzeErrorRecord;
import com.htd.htdapmservice.dao.LogAnalyzeErrorRecordDao;
import com.htd.htdapmservice.service.intf.OneAlertKeyService;
import com.htd.htdapmservice.service.intf.OneAlertService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;


@JobHandler(value="logErrorAlarmJobHandler")
@Component
public class LogErrorAlarmJobHandler extends IJobHandler {
    private static final Logger logger = LoggerFactory.getLogger(LogErrorAlarmJobHandler.class);

    @Autowired
    private LogAnalyzeErrorRecordDao logAnalyzeErrorRecordDao;

    @Autowired
    private OneAlertService  oneAlertService;

    @Autowired
    private OneAlertKeyService oneAlertKeyService;
 

	@Override
	public ReturnT<String> execute(String param) throws Exception {

        logger.info("logErrorAlarmJobHandler Entrance, param : {}", new Object[]{JSONObject.toJSONString(param)});
        List<LogAnalyzeErrorRecord> logAnalyzeErrorRecordList = new ArrayList<LogAnalyzeErrorRecord>();
        try{
            logAnalyzeErrorRecordList = logAnalyzeErrorRecordDao.findLogAnalyzeErrorRecordListByStatus(AlarmSendStatus.SEND_NO.getCode());
            
            if (logAnalyzeErrorRecordList!=null &&logAnalyzeErrorRecordList.size()>0) {
                for (LogAnalyzeErrorRecord logAnalyzeErrorRecord : logAnalyzeErrorRecordList) {
                    AlarmInto alarmInto = new AlarmInto();
                    alarmInto.setEventType(AlarmsStatusEnum.ALARM_TRIGGER.getCode());
                    alarmInto.setPriority(AlarmsLevelEnum.LEVEL_TWO.getCode());
                    alarmInto.setProductType(logAnalyzeErrorRecord.getProductTypeCode());
                    alarmInto.setAlarmsDetail(logAnalyzeErrorRecord.getAlarmsDetail());
                    alarmInto.setAlarmsTitle(oneAlertKeyService.findOneAlertKeyByProductCode(logAnalyzeErrorRecord.getProductTypeCode()).getProductName()+"日志异常");
                    AlarmResult alarmResult = oneAlertService.createAlarm(alarmInto);
                    if(ErrorCodeConstants.SUCCESS_CODE.equals(alarmResult.getResponseCode())){
                        logAnalyzeErrorRecord.setSendStatus(AlarmSendStatus.SEND_YES);
                        logAnalyzeErrorRecord.setUpdateTime(new Date());
                        logAnalyzeErrorRecordDao.updateLogAnalyzeErrorRecordById(logAnalyzeErrorRecord);
                    }
                }
            }
        } catch (Exception e){
            logger.error("logErrorAlarmJobHandler error:", e);
        }
        
		return null;
	}

}


